<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2018/12/25
 * Time: 12:13
 */

namespace api\storage\controller;


use api\storage\model\PurchaseDetailModel;
use api\storage\model\ReelsModel;
use api\storage\service\MaterielService;

class MaterielController extends AppBaseController
{

    /**
     * @api {GET} /api/storage/materiel 查找reelId对应的全部物料
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  index
     * @apiGroup  materiel
     * @apiVersion 0.0.1
     * @apiDescription 查找reelId对应物料号，再根据这个物料号把该仓库的reelId全部查找出来
     *
     * @apiParam {string} reelId 物料号
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     * @apiSuccess {Object} data
     * @apiSuccess {string} data.subMaterielId 物料号
     * @apiSuccess {Array} data.reels 物料信息
     * @apiError  Error
     **/
    public function index()
    {
        $reelId = input('reelId');
        $purchaseModel = new PurchaseDetailModel();
        //采购表中查询这个reelId所对应的物料号
        $materielId = $purchaseModel->findMaterielIdByReelId($reelId);
        if (empty($materielId)) {
            $this->error('reelId不在库中', $this->materielTemplate());
        }
        $reelsModel = new ReelsModel();
        //查找出在库中的（status=1）的同仓库物料
        $reels = $reelsModel->queryByMaterielIdAndUuid($materielId, $this->getStorageUuid());
        $this->success('查询成功', $this->materielTemplate($materielId, $reels->toArray()));
    }

    /**
     * @api {PUT} /api/storage/materiel/type_modify 修改物料类型
     * @apiHeader {string} XX-Device-Type 说明登录的设备 mobile \ android \ iphone \ ipad \ web \ pc \ mac \ wxapp
     * @apiHeader {string} XX-Token token信息，登录后返回
     * @apiHeader {string} XX-Api-Version api的版本信息
     * @apiName  modifyType
     * @apiGroup  materiel
     * @apiVersion 0.0.1
     * @apiDescription 修改物料的类型
     *
     * @apiParam {string} materielId 物料号
     * @apiParam {string} type 要修改的类型
     *
     * @apiSuccess {string} code 0
     * @apiSuccess {string} msg 请求成功
     **/
    public function modifyType()
    {
        $validate = $this->validate($this->request->param(), 'Materiel.modifyType');
        if ($validate !== true) {
            $this->error($validate);
        }
        $materielId = input('materielId');
        $newType = input('type');
        $service = new MaterielService();
        $result = $service->modifyType($materielId, $newType, $this->getStorageUuid());
        switch ($result['status']) {
            case -2:
                $this->error('该物料不存在');
                break;
            case -1:
                $this->error('该物料已有在货架上，不能修改类型');
                break;
            case 0:
                $this->error('修改类型失败');
                break;
            case 1:
                $this->success('修改类型成功');
                break;
            default:
                $this->error('请求失败');
        }
    }

    private function materielTemplate($materielId = '', $reels = [])
    {
        return [
            'subMaterielId' => $materielId,
            'reels' => $this->convertResult($reels)
        ];
    }
}